package com.glxy.wxtakeaway.mapper;

import com.glxy.wxtakeaway.entity.Users;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

//2.声明一个UserMapper接口
public interface UsersMapper {

    //通过openid查询是否用户存在
    @Select("SELECT CASE WHEN COUNT(*) > 0 THEN TRUE ELSE FALSE END AS result FROM users WHERE openid = #{openid}")
    boolean checkUserByOpenID(@Param("openid") String openid);

    //将用户写入数据库（携带openid）(注册一名新用户)
    @Update("INSERT INTO users(url,name,openid) VALUES(#{avatarUrl},#{nickName},#{openid})")
    void addUserByOpenId(@Param("avatarUrl") String avatarUrl,@Param("nickName") String nickName,@Param("openid") String openid);

    //根据openid查询用户信息
    @Select("select * from users where openid=#{openid}")
    List<Users> selectUserByOpenId(@Param("openid") String openid);

    //根据openid修改身份
    @Update("UPDATE users " +
            "SET identity = CASE " +
            "    WHEN identity = 'deliveryman' THEN 'pickup' " +
            "    WHEN identity = 'pickup' THEN 'deliveryman' " +
            "    ELSE identity " +
            "END " +
            "WHERE openid = #{openid}")
    Boolean changeUserByOpenId(@Param("openid") String openid);
}
